package com.ycp.erp.goods.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.widget.ClearEditView;
import com.one.common.view.widget.MyTitleBar;
import com.skateboard.zxinglib.CaptureActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ycp.erp.R;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

import static android.Manifest.permission.CAMERA;

/**
 * Class Des:
 * Created by bjh on 2021/8/26.
 */
@Route(path = RouterPath.ADD_GOODS)
public class AddGoodsActivity extends BaseActivity {
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    @BindView(R.id.ivADD)
    ImageView ivADD;
    @BindView(R.id.ivReal)
    ImageView ivReal;
    @BindView(R.id.ivScan)
    ImageView ivScan;
    @BindView(R.id.rlAddImg)
    RelativeLayout rlAddImg;
    @BindView(R.id.etGoodsName)
    ClearEditView etGoodsName;
    @BindView(R.id.etGoodsNO)
    ClearEditView etGoodsNO;
    @BindView(R.id.tvGoodsCategory)
    TextView tvGoodsCategory;
    @BindView(R.id.rlGoodsCategory)
    RelativeLayout rlGoodsCategory;
    @BindView(R.id.tvGoodsBrand)
    TextView tvGoodsBrand;
    @BindView(R.id.rlGoodsBrand)
    RelativeLayout rlGoodsBrand;
    @BindView(R.id.etGoodsBarCode)
    ClearEditView etGoodsBarCode;
    @BindView(R.id.etGoodsSpecification)
    ClearEditView etGoodsSpecification;
    @BindView(R.id.etGoodsUnit)
    ClearEditView etGoodsUnit;
    @BindView(R.id.tvGoodsValidPeriod)
    TextView tvGoodsValidPeriod;
    @BindView(R.id.rlGoodsValidPeriod)
    RelativeLayout rlGoodsValidPeriod;
    @BindView(R.id.etGoodsBuyPrice)
    ClearEditView etGoodsBuyPrice;
    @BindView(R.id.etGoodsSellPrice)
    ClearEditView etGoodsSellPrice;
    @BindView(R.id.etGoodsSort)
    ClearEditView etGoodsSort;
    @BindView(R.id.etGoodsInventoryWarning)
    ClearEditView etGoodsInventoryWarning;
    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("添加商品").hideLine();
    }
    @Override
    protected void initPresenter() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
        RxView.clicks(rlGoodsBrand).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            RouterManager.getInstance().go(RouterPath.BRAND_MANAGER);
        });
        RxView.clicks(rlGoodsCategory).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            RouterManager.getInstance().go(RouterPath.CATEGORY_MANAGER);
        });
        RxView.clicks(ivScan).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    CAMERA)
                    .subscribe(aBoolean -> {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            Intent intent=new Intent(AddGoodsActivity.this, CaptureActivity.class);
                            startActivityForResult(intent,1001);
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权相机权限，请在设置中打开授权");
                            finishPage();
                        }
                    });
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1001 && resultCode== Activity.RESULT_OK)
        {
            String result=data.getStringExtra(CaptureActivity.KEY_DATA);
            etGoodsBarCode.setText(result);
        }
    }
    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_goods;
    }

}
